
#include "hardware.h"
#include "sys.h"
#include "debug.h"
#include "status.h"
#include "macros.h"
#include "atd.h"

#include "FreeRTOS.h"
#include "task.h"


#define   SAMPLE_RATE	20   /* msec */
#define   INP_TRK_DEBOUNCE_MS					(200/SAMPLE_RATE) 

#define   INP_HIGH		1
#define   INP_LOW			0

unsigned char debcnt_trk = 0;

void gpio_task ( void *pvParameters )
{
	portTickType xLastWakeTime = xTaskGetTickCount();

	PIN_TRK_DIR = PIN_INPUT;
	PIN_OIL_SPEED_DIR = PIN_INPUT;

	
	for(;;)
	{
		vTaskDelayUntil( &xLastWakeTime, ( SAMPLE_RATE MSEC ));
	
		/* detect level of PIN_TRK input */
		if ( dev_status.trk == 0 )
		{
			if ( PIN_TRK == INP_HIGH )
			{
				if ( ++debcnt_trk > INP_TRK_DEBOUNCE_MS )
				{
					debcnt_trk = 0;

					dev_status.trk = 1;
					
					dbg("dev_status.trk: %s", dev_status.trk? "HIGH":"LOW"); //Dima
				}
			}
			else
			{
				debcnt_trk = 0;
			}
		} 
		else
		{
			if ( PIN_TRK == INP_LOW )
			{
				if ( ++debcnt_trk > INP_TRK_DEBOUNCE_MS )
				{
					debcnt_trk = 0;

					dev_status.trk = 0;
					
					dbg("dev_status.trk: %s", dev_status.trk? "HIGH":"LOW"); //Dima
				}
			}
			else
			{
				debcnt_trk = 0;
			}
		}

		
	}
}
